package com.sergivonavi.materialbanner.app.slice;

import com.sergivonavi.materialbanner.Banner;
import com.sergivonavi.materialbanner.app.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;
import ohos.global.resource.NotExistException;
import ohos.global.resource.WrongTypeException;
import java.io.IOException;

public class StyledBannerAbilitySlice extends AbilitySlice {
    private ComponentContainer mStyledRoot;

    @Override
    protected void onStart(Intent intent) {
        try {
            int color = getResourceManager().getElement(ResourceTable.Color_title_bar_bg).getColor();
            getWindow().setStatusBarColor(color);
        } catch (IOException | NotExistException | WrongTypeException e) {
            e.printStackTrace();
        }
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_sample_ability_styled_banner);

        Component hint = findComponentById(ResourceTable.Id_tx_hint);
        if (hint instanceof Text) {
            Text txHint = (Text) hint;
            txHint.setText(String.format(
                    "This multicolor styling is done only for the " +
                            "showcase purposes.%n Don't do this in your real apps :)"));
        }
        Component styled = findComponentById(ResourceTable.Id_styled_root);
        if (styled instanceof ComponentContainer) {
            mStyledRoot = (ComponentContainer) styled;
        }
        Banner banner = new Banner.Builder(this)
                .setParent(mStyledRoot)
                .setIcon(ResourceTable.Media_ic_signal_wifi_off , false)
                .setMessage(ResourceTable.String_banner_message3)
                .setLeftButton(ResourceTable.String_banner_btn_left,
                        ResourceTable.Graphic_style_btn_ripple_bg, null)
                .setRightButton(ResourceTable.String_banner_btn_right,
                        ResourceTable.Graphic_style_btn_ripple_bg, null)
                .create();
        try {
            int bgColor = this.getResourceManager().getElement(
                    ResourceTable.Color_custom_background).getColor();
            ShapeElement shapeElement = new ShapeElement();
            shapeElement.setRgbColor(RgbColor.fromArgbInt(bgColor));
            banner.setBackground(shapeElement);

            int lineColor = this.getResourceManager().getElement(
                    ResourceTable.Color_custom_line).getColor();
            banner.setLineColor(lineColor);
        } catch (IOException | NotExistException | WrongTypeException e) {
            e.printStackTrace();
        }
        banner.setMessageTextColor(ResourceTable.Color_custom_message_text);
        banner.setButtonsTextColor(ResourceTable.Color_custom_buttons_text);
        banner.setButtonTextIsBold(true);
        banner.setLineOpacity(0.8f);
        // And then show this banner
        banner.show();
    }
}
